package park.gz.com.parkingfee.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import park.gz.com.parkingfee.R;
import park.gz.com.parkingfee.bean.FeeHistory;
import park.gz.com.parkingfee.until.ShareUtil;


public class FeeDetailActivity extends Activity {

    @Bind(R.id.outapplication)
    TextView outapplication;
    @Bind(R.id.relativeLayout)
    RelativeLayout relativeLayout;
    @Bind(R.id.tx_style)
    TextView txStyle;
    @Bind(R.id.tx_status)
    TextView txStatus;
    @Bind(R.id.tx_parktime)
    TextView txParktime;
    @Bind(R.id.fee)
    TextView fee;
    @Bind(R.id.fact_fee)
    TextView factFee;
    @Bind(R.id.updatetime)
    TextView updatetime;
    @Bind(R.id.in_admin)
    TextView inAdmin;
    @Bind(R.id.input_img)
    ImageView inputImg;
    @Bind(R.id.in_carno)
    TextView inCarno;
    @Bind(R.id.in_time)
    TextView inTime;
    @Bind(R.id.out_admin)
    TextView outAdmin;
    @Bind(R.id.out_img)
    ImageView outImg;
    @Bind(R.id.out_carno)
    TextView outCarno;
    @Bind(R.id.outtime)
    TextView outtime;
    private FeeHistory feedetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedetail);
        ButterKnife.bind(this);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int widthPixels = metrics.widthPixels;
        int heightPixels = metrics.heightPixels;
        ViewGroup.LayoutParams params = inputImg.getLayoutParams();
        params.height = (widthPixels - 20) / 16 * 9;
        params.width = widthPixels - 20;
        inputImg.setLayoutParams(params);
        outImg.setLayoutParams(params);
        feedetail = (FeeHistory) getIntent().getSerializableExtra("feedetail");
        txParktime.setText("停车时长：" + minConvertDayHourMin(Double.parseDouble(feedetail.getParked_time())));
        inTime.setText("入场时间：" + feedetail.getIn_time());
        outtime.setText("出场时间：" + feedetail.getOut_time());
        fee.setText("应收金额：" + feedetail.getFee());
        factFee.setText("实收金额：" + feedetail.getFact_fee());
        inCarno.setText(feedetail.getCar_no());
        outCarno.setText(feedetail.getCar_no());
        inAdmin.setText(!feedetail.getIn_operator().equals("null") ? "操作员：" + feedetail.getIn_operator() : "操作员：未知");
        outAdmin.setText(!feedetail.getOut_operator().equals("null") ? "操作员：" + feedetail.getOut_operator() : "操作员：未知");
        updatetime.setText(!feedetail.getUpdated_at().equals("null") ? "更新时间：" + feedetail.getUpdated_at() : "更新时间：未知");
        txStatus.setText(!feedetail.getStatus().equals("null") ? "车辆状态：" + feedetail.getStatus() : "车辆状态：未知");
        txStyle.setText(!feedetail.getCard_type().equals("null") ? "车辆类型：" + feedetail.getCard_type() : "车辆类型：未知");

        Glide.with(this).
                load(ShareUtil.getstringvalue(FeeDetailActivity.this,"baseurl") +"/car_images/"+ feedetail.getIn_time().split(" ")[0] + "/" + feedetail.getIn_image()).
                asBitmap().//强制处理为bitmap
                placeholder(R.drawable.ic_img_car).//加载中显示的图片
                error(R.drawable.ic_img_car).//加载失败时显示的图片
                into(inputImg);//显示到目标View中
        Glide.with(this).
                load(ShareUtil.getstringvalue(FeeDetailActivity.this,"baseurl") +"/car_images/"+ feedetail.getOut_time().split(" ")[0] + "/" + feedetail.getOut_image()).
                asBitmap().//强制处理为bitmap
                placeholder(R.drawable.ic_img_car).//加载中显示的图片
                error(R.drawable.ic_img_car).//加载失败时显示的图片
                into(outImg);//显示到目标View中
    }

    public static String minConvertDayHourMin(Double min) {
        String html = "0分";
        if (min != null) {
            Double m = (Double) min;
            String format;
            Object[] array;
            Integer days = (int) (m / (60 * 24));
            Integer hours = (int) (m / (60) - days * 24);
            Integer minutes = (int) (m - hours * 60 - days * 24 * 60);
            if (days > 0) {
                format = "%1$,d天%2$,d时%3$,d分";
                array = new Object[]{days, hours, minutes};
            } else if (hours > 0) {
                format = "%1$,d时%2$,d分";
                array = new Object[]{hours, minutes};
            } else {
                format = "%1$,d分";
                array = new Object[]{minutes};
            }
            html = String.format(format, array);
        }
        return html;
    }

    @OnClick(R.id.outapplication)
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.outapplication:
                Intent intent = new Intent(FeeDetailActivity.this,MainActivity.class);
                startActivity(intent);
                break;
        }
    }
}

